package com.group.Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group.ForceLogoutUtils.ForceLogoutUtils;
import com.group.Listener.SessionListener;
import com.group.pojo.GUser;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public LoginServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求动作
		String action = request.getParameter("action");
		if(action.equals("myInfo")) {
			HttpSession session = request.getSession();
			GUser user = (GUser)session.getAttribute("SESSION_USER");
			String name = user.getNameByID(user);
			request.setAttribute("name", name);
			request.getRequestDispatcher("self.jsp").forward(request, response);
			
		}

	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取请求参数
		String userID = request.getParameter("userID");
		String password = request.getParameter("password");
		String rememberMe = request.getParameter("rememberMe");
		GUser user = new GUser(userID, password);
		HttpSession session = request.getSession();  
		//账号信息正确
		if(user.login()) {
	        //该账号已经被登陆 
	        if(null != SessionListener.sessionMap.get(user.getUserID())){  
	            //将已经登陆的信息拿掉,将新的用户登录信息放进去 
	            ForceLogoutUtils.forceUserLogout(user.getUserID()); 
	            SessionListener.sessionMap.put(user.getUserID(), session );  
	        }  
	        //该账号未被登陆 
	        else{  
	            SessionListener.sessionMap.put(user.getUserID(), session );
	        }  
	        session.setAttribute("SESSION_USER", user);
			if(null != user.isTeacher(userID)) request.getSession().setAttribute("SESSION_TEACHER", user.isTeacher(userID));
			
			rememberMe(rememberMe, userID, password, request, response);
			response.sendRedirect("ClassInfoServlet?action=info");
		}
		//账号信息错误
		else {
			
		}
		
		
	}//POST
	
	
	private void rememberMe(String rememberMe, String userID, String password, HttpServletRequest request, HttpServletResponse response) {
		//判断是否需要通过cookie记住账号和密码
		if("true".equals(rememberMe)) {
			//记住账号及密码
			Cookie cookie = new Cookie("COOKIE_USERID",userID);
			cookie.setPath("/");
			cookie.setMaxAge(2 * 60 * 60);
			response.addCookie(cookie);
			cookie = new Cookie("COOKIE_PASSWORD",password);
			cookie.setPath("/");
			cookie.setMaxAge(2 * 60 * 60);
			response.addCookie(cookie);
		} else {
			//将账号及密码cookie删除
			Cookie[] cookies = request.getCookies();
			if(cookies != null) {
				for(Cookie cookie : cookies) {
					if("COOKIE_USERID".equals(cookie.getName()) || "COOKIE_PASSWORD".equals(cookie.getName())) {
						cookie.setMaxAge(0);
						cookie.setPath("/");
						response.addCookie(cookie);
					}
				}
			}
		}
	}//remember
}
